#include <Arduino.h>
#include <ESP8266WiFi.h>


const char* ssid = "your-wifi";
const char* password = "your-wifi-password";
const char* host = "maker.ifttt.com";
const char* eventName = "button_pressed"; 
const char* key = "your-key";


void setup() 
{
  Serial.begin(115200);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED) 
  {    
    delay(500);
    Serial.print("."); 
  }
  Serial.println("");
  Serial.println("WiFi connected");    
  Serial.println("IP address: ");  
  Serial.println(WiFi.localIP());
  // Pin D6 as input 
  pinMode(D6, INPUT);
}


void loop()
{
  if (digitalRead(D6))
  {
    WiFiClient client;// Use WiFiClient class to create TCP connections 
    const int httpPort = 80;
    if (!client.connect(host, httpPort))
    {
      Serial.println("connection failed");   
      return;
    }  

    // We now create a URI for the request  
    String url = "/trigger/";
    url += eventName;
    url += "/with/key/"; 
    url += key;  
    Serial.print("Requesting URL: ");  
    Serial.println(url);

    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                              "Host: " + host + "\r\n" + 
                              "Connection: close\r\n\r\n");
    int timeout = millis() + 5000;

    while (client.available() == 0) //判断超时时间
    {   
      if (timeout - millis() < 0) 
      {
        Serial.println(">>> Client Timeout !");      
        client.stop();
        return;   
      }
    }

    while(client.available())//打印返回的网络应答
    {
      String line = client.readStringUntil('\r');    
      Serial.print(line);
    }  

    Serial.println();
    Serial.println("closing connection");
  }


}